package com.rainwood.water_authority.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.android.zxing.android.QRCodeCaptureActivity;
import com.rainwood.water_authority.R;
import com.rainwood.water_authority.aop.SingleClick;
import com.rainwood.water_authority.base.MyBaseActivity;
import com.rainwood.water_authority.ui.dialog.MessageDialog;
import com.rainwood.water_authority.util.Constants;
import com.rainwood.water_authority.util.LogUtils;
import com.sxs.tools.permission.OnPermission;
import com.sxs.tools.permission.Permission;
import com.sxs.tools.permission.XXPermissions;
import com.sxs.tools.sqlite.PreferenceUtil;

import java.util.List;

import static com.rainwood.water_authority.util.Constants.IN_STOCK_NUM;

/**
 * @author sxs
 * @date :2020/9/28
 * @desc: 扫码入库结束
 */
public final class InStockFinishAty extends MyBaseActivity {

    private TextView mScanNumView;

    public static void start(Context context){
        Intent intent = new Intent(context, InStockFinishAty.class);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_in_stock_finished;
    }

    @Override
    protected void initView() {
        mScanNumView = findViewById(R.id.tv_scan_num);

        setOnClickListener(R.id.btn_finished, R.id.btn_continue);
    }

    @Override
    protected void initData() {
        int inStockNum = PreferenceUtil.with(getContext()).getInt("in_stock_num", 0);
        mScanNumView.setText(String.valueOf(inStockNum));
    }

    @SingleClick
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_finished:
                new MessageDialog.Builder(this)
                        .setTitle("扫码结束")
                        .setMessage("结束扫码将自动创建入库登记单，您确定要这样做吗？")
                        .setListener(dialog -> {
                            IN_STOCK_NUM = 0;
                            PreferenceUtil.with(getContext()).put("in_stock_num", IN_STOCK_NUM);
                            postDelayed(() -> {
                                ShouLiaoHomeAct.start(getContext());
                                finish();
                            }, 100);
                        }).show();
                break;
            case R.id.btn_continue:
                XXPermissions.with(this)
                        .permission(Permission.CAMERA)
                        .permission(Permission.Group.STORAGE)
                        .request(new OnPermission() {
                            @Override
                            public void hasPermission(List<String> granted, boolean all) {
                                if (all) {
                                    //跳转扫码页面
                                    Intent intent = new Intent(getContext(), QRCodeCaptureActivity.class);
                                    intent.putExtra(QRCodeCaptureActivity.STATUS_BAR_COLOR, Color.parseColor("#99000000"));
                                    startActivityForResult(intent, Constants.REQUEST_SCAN_CODE);
                                }
                            }

                            @Override
                            public void noPermission(List<String> denied, boolean quick) {
                                if (quick) {
                                    toast(R.string.common_permission_fail);
                                    XXPermissions.startPermissionActivity(getActivity(), false);
                                } else {
                                    toast(R.string.common_permission_hint);
                                }
                            }
                        });
                // ScanTreasuryAty.start(this);
                // finish();
                break;
            default:break;
        }
    }

    /**
     * 获取扫码结果
     */
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Constants.REQUEST_SCAN_CODE && resultCode == RESULT_OK) {
            Bundle bundle = data.getExtras();
            String result = null;
            if (bundle != null) {
                result = bundle.getString(QRCodeCaptureActivity.CODE_CONTENT);
                LogUtils.d(TAG, "-------- result-------- " + result);
            }
            if (TextUtils.isEmpty(result)) {
                toast("未识别到任何内容");
                return;
            }
            ShouLiaoListAct.start(this, result);
        }
    }
}